package com.icloud.common.core.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 精度计算工具类
 *
 * @author pujiulin
 * @date 2022-12-30
 */
public class BigDecimalUtils {

	/**
	 * 保留小数点后n位
	 * @param a
	 * @param n
	 * @return
	 */
	public static BigDecimal round(BigDecimal a, int n, int scale) {
		if(null == a){
			return BigDecimal.ZERO;
		}
		a = a.setScale(n, scale);
		return a;
	}
	/**
	 * 四舍五入保留2位小数
	 * @param a
	 * @return
	 */
	public static BigDecimal rounding(BigDecimal a) {
		return round(a, 2, BigDecimal.ROUND_HALF_UP);
	}
	/**
	 * 四舍五入保留n位小数
	 * @param a
	 * @return
	 */
	public static BigDecimal rounding(BigDecimal a,int n) {
		return round(a, n, BigDecimal.ROUND_HALF_UP);
	}
	/**
	 * 截取保留4位小数
	 * @return
	 */
	public static BigDecimal intercept(BigDecimal a) {
		return round(a, 4, BigDecimal.ROUND_DOWN);
	}
	/**
	 * 获取百分比
	 * @param numerator 分子
	 * @param denominator 分母
	 * @return
	 */
	public static String getPercent(int numerator, int denominator){
		DecimalFormat df = new DecimalFormat("##.##");
		return denominator != 0 ?
				df.format((double) numerator/ denominator * 100) + "%" : "0.00%";
	}

	public static String getPercent(BigDecimal numerator,BigDecimal denominator){
		BigDecimal percent = BigDecimalUtils.multiply(BigDecimalUtils.divide(numerator,denominator),new BigDecimal(100));
		return  BigDecimalUtils.rounding(percent,2) + "%";
	}
	/**
	 * 金额累加
	 * @return
	 */
	public static BigDecimal add(BigDecimal a, BigDecimal b){
		if(null == a && null == b){
			return BigDecimal.ZERO;
		}
		if(a == null){
			return b;
		}
		if(null == b){
			return a;
		}
		return a.add(b);
	}

	/**
	 * 取和
	 * @return
	 */
	public static BigDecimal sum(List<BigDecimal> a){

		BigDecimal sum = BigDecimal.ZERO;
		for (BigDecimal bigDecimal : a) {
			sum = add(sum,bigDecimal);
		}
		return sum;
	}

	/**
	 * 字符串求和
	 * @param list
	 * @return
	 */
	public static BigDecimal sumString(List<String> list){

		BigDecimal sum = BigDecimal.ZERO;
		for (String s : list) {
			sum = add(sum,new BigDecimal(s));
		}
		return sum;
	}

	/**
	 * 金额相减
	 * @return
	 */
	public static BigDecimal subtract(BigDecimal a, BigDecimal b){
		if (null == a){
			return BigDecimal.ZERO;
		}
		if (null == b){
			return a;
		}
		return a.subtract(b);
	}
	/**
	 * 金额相除
	 * @return
	 */
	public static BigDecimal divide(BigDecimal a, BigDecimal b){
		if(null == a || BigDecimal.ZERO.equals(a)){
			return BigDecimal.ZERO;
		}
		BigDecimal result = a.divide(b, 4, BigDecimal.ROUND_DOWN);
		return result;
	}

	/**
	 * 相除
	 * @param a 被除数
	 * @param b 除数
	 * @param scale 保留几位小数
	 * @param roundingMode 舍入模式
	 * @return
	 */
	public static  BigDecimal divide(BigDecimal a, BigDecimal b,int scale, RoundingMode roundingMode){
		if(null == a || BigDecimal.ZERO.equals(a)){
			return BigDecimal.ZERO;
		}
		BigDecimal result = a.divide(b, scale, roundingMode);
		return result;
	}

	public static BigDecimal divideOneHundred(BigDecimal a){
		return divide(a,new BigDecimal(100));
	}

	public static void main(String[] args) {
		System.out.println(divide(new BigDecimal(1), new BigDecimal(4)));
	}
	/**
	 * 金额相乘
	 * @return
	 */
	public static BigDecimal multiply(BigDecimal a, BigDecimal b){
		if (null == a || null == b){
			return BigDecimal.ZERO;
		}

		BigDecimal result = a.multiply(b);
		return result;
	}

	/**
	 * 计算平均值，保留两位小数
	 * @param bigDecimals
	 * @return
	 */
	public static BigDecimal average(List<BigDecimal> bigDecimals){
		BigDecimal sum = sum(bigDecimals);
		BigDecimal result = sum.divide(new BigDecimal(bigDecimals.size()), 2, BigDecimal.ROUND_DOWN);
		return result;
	}

	/**
	 * 大于等于
	 *
	 * @param source
	 * @param target
	 * @return
	 */
	public static boolean gte(BigDecimal source, BigDecimal target) {
		if (source == null) {
			return false;
		}
		if (target == null) {
			target = BigDecimal.ZERO;
		}
		if (source.compareTo(target) > -1) {
			return true;
		}
		return false;
	}

	/**
	 * 大于
	 *
	 * @param source
	 * @param gt
	 * @return
	 */
	public static boolean gt(BigDecimal source, BigDecimal gt) {
		if (source == null) {
			return false;
		}
		if (gt == null) {
			gt = BigDecimal.ZERO;
		}
		if (source.compareTo(gt) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 小于
	 *
	 * @param source
	 * @param less
	 * @return
	 */
	public static boolean lt(BigDecimal source, BigDecimal less) {
		if (source == null) {
			return false;
		}
		if (less == null) {
			less = BigDecimal.ZERO;
		}
		if (source.compareTo(less) < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 小于等于
	 *
	 * @param source
	 * @param less
	 * @return
	 */
	public static boolean lte(BigDecimal source, BigDecimal less) {
		if (source == null) {
			return false;
		}
		if (less == null) {
			less = BigDecimal.ZERO;
		}
		if (source.compareTo(less) <= 0) {
			return true;
		}
		return false;
	}
	/**
	 * 小于等于
	 *
	 * @param source
	 * @param less
	 * @return
	 */
	public static boolean eq(BigDecimal source, BigDecimal less) {
		if (source == null) {
			return false;
		}
		if (less == null) {
			less = BigDecimal.ZERO;
		}
		if (source.compareTo(less) == 0) {
			return true;
		}
		return false;
	}


}
